<template>
  <div class="disasterWarningWrap">
    <van-nav-bar
      :title="`${title}预警`"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      
    >
      <div slot="right">
        <van-popover
          v-model="showPopover"
          trigger="click"
          :actions="actions1"
          @select="onSelect1"
        >
          <template #reference>
            <img
              src="../assets/Earlywarningmap/gd.png"
              style="display: block;width: 20px;height: 18px;"
              alt=""
            />
          </template>
        </van-popover>
      </div>
    </van-nav-bar>
    <div class="tabtop">
      <div :class="active == 1 ? 'bendi' : 'bendiactive'" @click="qiehuan(0)">
        <img
          v-if="active != 1"
          src="../assets/Earlywarningmap/yujing.png"
          alt=""
        />
        <img v-else src="../assets/Earlywarningmap/yujingactive.png" alt="" />
        <div>本地预警</div>
      </div>
      <div
        :class="active == 0 ? 'quansehng' : 'quansehngactive'"
        @click="qiehuan(1)"
      >
        <img
          v-if="active != 0"
          src="../assets/Earlywarningmap/yujing.png"
          alt=""
        />
        <img v-else src="../assets/Earlywarningmap/yujingactive.png" alt="" />
        <div>全省预警</div>
      </div>
    </div>
    <div
      class="listArea"
      v-scrollBottom="scrollBottomMet"
      infinite-scroll-distance="10"
    >
      <div
        class="listLine"
        v-for="(item, index) in listData"
        :key="Number(index)"
        @click="toDetailFile(item)"
      >
        <div class="listLeft">
          <img
            :src="
              require(`@/assets/Earlywarningmap/yujingmap/${item.imageType}-${item.degree}.png`)
            "
            alt=""
          />
        </div>
        <div class="listRight">
          <div class="listCon">
            <span>{{ item.alarmHeadline }}</span>
          </div>
          <div class="listTime">
            <span>有效日期：</span><span>{{ item.alarmEffective }}</span>
          </div>
        </div>
      </div>
      <div v-if="noData" class="noData"><span>暂无更多数据</span></div>
    </div>

    <van-popup
      v-model="showDetail"
      position="right"
      :style="{ width: '100%', height: '100%' }"
    >
      <detail
        v-if="showDetail"
        @detailBack="showDetail = false"
        :nowClickItem="nowClickItem"
        :yjlist="yjlist"
      >
      </detail>
    </van-popup>

    <van-action-sheet
      v-model="showActionSheet"
      :actions="actions"
      cancel-text="取消"
      close-on-click-action
      @cancel="showActionSheet = false"
      @select="onSelect"
    />
    <comArea
      v-if="showComArea"
      @closeEmit="showComArea = false"
      @confirmEmit="confirmEmitAreaMet"
    ></comArea>
    <comCity
      v-if="showComCity"
      @confirmEmit="confirmEmitCityMet"
      @closeEmit="showComCity = false"
    ></comCity>
  </div>
</template>
<script type="text/javascript">
import meteoroDisasterWarningApi from '@/service/disasterWarning.js'
import detail from '@/components/disasterWarning/detail.vue'
//市级与市区级 选择ccc
import comArea from '@/components/common/area.vue'
import comCity from '@/components/common/city.vue'
import Vue from 'vue'
import { Tab, Tabs, Popover } from 'vant'
import { getItem, setItem } from '../util/storage.js'
Vue.use(Popover)
Vue.use(Tab)
Vue.use(Tabs)
export default {
  data() {
    return {
      active: 0,
      total: 0,
      listData: [],
      page: {
        limit: 20,
        page: 1,
      },
      actives: 0,
      showPopover: false,
      // 通过 actions 属性来定义菜单选项
      actions1: [{ text: '选择区域' }, { text: '预警地图' }],
      noData: false,
      isGetRoute: true,
      title: '',
      showDetail: false,
      nowClickItem: {},
      //选择市区级/市级
      showActionSheet: false,
      actions: [{ name: '市级' }, { name: '市区级' }],
      showComArea: false,
      showComCity: false,
      city: '',
      cnty: '',
      popu: false,
      yjlist: '',
      location: [],
    }
  },
  created() {
    this.location = getItem('location')
    if (typeof this.$route.query.tab != 'undefined') {
      //   this.active = Number(this.$route.query.tab)
      this.changeTab()
    } else {
      this.city = this.location[1].name
      this.cnty = this.location[2].name
      this.title = this.cnty ? this.cnty : this.city

      this.initApi()
    }
  },
  components: {
    detail,
    comCity,
    comArea,
  },
  methods: {
    onSelect1(action) {
      console.log(action.text)
      //   this.$toast(action.text)
      if (action.text == '选择区域') {
        this.showActionSheet = true
      } else {
        this.$router.push('dwWarnEarth')
      }
    },
    popclaick() {
      this.popu = !this.popu
    },
    changeTab() {
      if (this.active == 0) {
        //重置
        this.reset()
        this.title = this.location[1].name
      } else if (this.active == 1) {
        //重置
        // this.reset()
        this.title = '山西省'
      }

      //请求
      this.initApi()
    },
    reset() {
      this.page.page = 1
      this.page.limit = 20
      this.total = 0
      this.listData = []
      this.noData = false
    },
    onClickLeft() {
      this.$router.push('/dwWarnEarth')
    },
    onClickRight() {
      this.$router.push('dwWarnEarth')
    },
    toDetailFile(item) {
      this.yjlist = '预警列表'
      this.nowClickItem = item
      this.showDetail = true
    },
    getDateStr(AddDayCount) {
      var dd = new Date()
      dd.setDate(dd.getDate() + AddDayCount) //获取AddDayCount天后的日期
      var y = dd.getFullYear()
      var m = dd.getMonth() + 1 //获取当前月份的日期

      var d = dd.getDate()
      d = d < 10 ? '0' + d : d
      var h = dd.getHours()
      h = h < 10 ? '0' + h : h
      var minute = dd.getMinutes()
      var second = dd.getSeconds()
      minute = minute < 10 ? '0' + minute : minute
      second = second < 10 ? '0' + second : second
      return (
        y +
        '-' +
        (m < 10 ? '0' + m : m) +
        '-' +
        d +
        ' ' +
        h +
        ':' +
        minute +
        ':' +
        second
      )
    },
    initApi() {
      //获取当前位置
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0,
      })
      if (this.active == 0) {
        if (this.cnty == '' && this.city == '') {
          this.city = this.location[1].name
          this.cnty = this.location[2].name
        }
        var a = this.getDateStr(-1)
        this.alarmSendtimeEnd = this.$util.getTime()
        this.alarmSendtimeStart = a
        var params = {
          city: this.city,
          alarmSendtimeStart: this.alarmSendtimeStart,
          alarmSendtimeEnd: this.alarmSendtimeEnd,
          cnty: this.cnty,
        }
      } else if (this.active == 1) {
        var params = {
          alarmSendtimeStart: this.alarmSendtimeStart,
          alarmSendtimeEnd: this.alarmSendtimeEnd,
        }
      }
      meteoroDisasterWarningApi
        .get_tb_alarm_findByCon(params)
        .then((res) => {
          console.log(res)
          if (res.length != 0) {
            if (this.listData.length == 0) {
              this.listData = this.$util.getDwType(res)
            } else {
              var formatArr = this.$util.getDwType(res)
              formatArr.forEach((item) => {
                this.listData.push(item)
              })
            }
            this.total = res.total
            if (this.listData.length >= this.total) {
              this.noData = true
            }
          } else {
            this.noData = true
          }
          this.$toast.clear()
        })
        .catch((error) => {
          this.$toast.clear()
        })
    },
    scrollBottomMet() {
      if (this.noData) {
        return
      }
      //下一页
      this.page.page++
      //请求
      this.initApi()
    },
    tabClick(name, title) {
      console.log(name)
      console.log(title)
      this.changeTab()
    },
    selectAreaMet() {
      this.showActionSheet = true
    },
    //地区选择器
    onSelect(item) {
      this.showActionSheet = false
      if (item.name == '市级') {
        this.showComCity = true
      } else if (item.name == '市区级') {
        this.showComArea = true
      }
    },
    confirmEmitAreaMet(value) {
      this.showComArea = false
      this.city = value[1].name
      this.cnty = value[2].name
      this.title = this.cnty
      //重置
      this.reset()
      //请求
      this.initApi()
    },
    confirmEmitCityMet(value) {
      this.showComCity = false
      this.city = value
      this.cnty = ''
      //重置
      this.reset()
      //请求
      this.title = this.city
      this.initApi()
    },
    qiehuan(e) {
      //   var d = document.querySelector('.leftMyButton')
      //   var b = document.querySelector('.RightMyButton')
      //   if (e == 0) {
      //     d.style.backgroundColor = '#1c61ee'
      //     b.style.backgroundColor = ''
      //   } else {
      //     d.style.backgroundColor = ''
      //     b.style.backgroundColor = '#1c61ee'
      //   }

      this.active = e

      this.changeTab()
      // this.active = index;
      // console.log(index);
    },
  },
}
</script>
<style type="text/css" lang="less" scoped>
.disasterWarningWrap {
  .pageCover();
  background: #f7f8fb;
  background-repeat: no-repeat;
  // background-size: cover;

  overflow-y: auto;
  .yjmap,
  .qy {
    color: #fff;
  }
  .yjmap {
    color: #fff;
    margin-left: 10px;
  }

  .selectArea {
    position: absolute;
    top: 5px;
    right: 80px;
    z-index: 1;
    text-align: center;
    font-size: 14px;
    color: white;
    padding: 10px 0;
  }
  .listArea {
    position: absolute;
    left: 10px;
    right: 10px;
    top: 100px;
    // bottom:60px;
    height: 85%;
    overflow-y: auto;
    background: white;
    border-radius: 10px;
    .listLine {
      overflow: hidden;
      padding: 15px 25px;
      border-bottom: 1px solid #ebedf0;
    }
    .listLeft {
      display: inline-block;
      vertical-align: middle;
      img {
        width: 30px;
      }
    }
    .listRight {
      vertical-align: middle;
      display: inline-block;
      font-size: 16px;
      margin-left: 10px;
      width: 85%;
      .listCon {
        margin-bottom: 10px;
      }
      .listTime {
        font-size: 12px;
        color: #999;
      }
    }
  }
  .van-tabs {
    position: absolute;
    left: 0;
    bottom: 0;
    right: 0;
  }
}
/deep/ .van-popover__arrow {
  left: 80%;
}
/deep/ .van-popover--light {
  width: 80px !important;
}
.tabtop {
  display: flex;
  justify-content: space-evenly;
  height: 45px;
  background: #ffffff;
  box-shadow: 0px 1px 7px 0px rgba(0, 0, 0, 0.08);
  .quansehng {
    display: flex;
    font-size: 14px;
    line-height: 45px;
    font-weight: 500;
    color: #656b75;

    align-items: center;
    img {
      display: block;

      width: 20px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .bendi {
    display: flex;
    font-size: 14px;
    align-items: center;
    line-height: 45px;
    font-weight: 500;
    color: #656b75;

    img {
      display: block;
      width: 20px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .quansehngactive {
    display: flex;
    font-size: 14px;
    line-height: 45px;
    font-weight: 500;
    color: #357cff;
    align-items: center;
    div {
      border-bottom: 2px solid #357cff;
    }
    img {
      display: block;

      width: 20px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .bendiactive {
    display: flex;
    font-size: 14px;
    align-items: center;
    line-height: 45px;
    font-weight: 500;
    color: #357cff;
    div {
      border-bottom: 2px solid #357cff;
    }
    img {
      display: block;
      width: 20px;
      height: 20px;
      margin-right: 3px;
    }
  }
}

/deep/.van-nav-bar__content {
  background: #357cff !important;
}
/deep/ .van-popover__content {
  width: 100px !important;
}
/deep/ .van-popover__action {
  width: 100px !important;
}

.LiveDatabottomTabbar {
  position: absolute;
  .px2rem(left, 20px);
  .px2rem(right, 20px);
  top: 92%;
  bottom: 30px;
  height: 60px;
  overflow: hidden;
  font-size: 18px;
  .leftMyButton {
    float: left;
    padding: 7px;
  }

  .RightMyButton {
    float: right;
    padding: 7px;
  }
}
/deep/ .van-action-sheet__cancel {
  height: 50px;
}
/deep/ .van-action-sheet__item {
  height: 50px;
}
.bottomTabbar {
  position: absolute;
  // .px2rem(left,10px);
  // .px2rem(right,1px);
  top: 810px;
  bottom: 10px;
  height: 45px;
  width: 100%;
  overflow: hidden;
  line-height: 45px;
  display: flex;
  font-size: 16px;
  .dbtn1 {
    display: flex;
    color: white;

    margin: 0px;
    background: url('../assets/baseButton.png');
    background-size: 100% 100%;
    width: 45%;
    border-radius: 10px;
    margin-left: 10px;
    span {
      margin-left: 10px;
    }
    img {
      margin: 10px 0 0 20px;
      width: 25px;
      height: 25px;
    }
  }
  .dbtn2 {
    display: flex;
    color: white;
    margin: 0px;
    background: url('../assets/baseButton.png');
    background-size: 100% 100%;
    width: 45%;
    border-radius: 10px;
    margin-left: 10px;
    span {
      margin-left: 10px;
    }
    img {
      margin: 10px 0 0 20px;
      width: 25px;
      height: 25px;
    }
  }
}
</style>
